﻿using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Server
    {
        static void Main(string[] args)
        {
            bool l = true;
            try
            {
                IPAddress adr = IPAddress.Parse("192.168.1.75");

                TcpListener listener = new TcpListener(adr, 7979);

                listener.Start();

                Console.WriteLine("Theserver is running at port 7979...");
                Console.WriteLine("The local End point is :" + listener.LocalEndpoint);
                Console.WriteLine("Waiting for connections ...");

                do
                {
                    Socket s = listener.AcceptSocket();
                    Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                    byte[] b = new byte[100];
                    int k = s.Receive(b);
                    Console.WriteLine("Received message : ");
                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(b[i]));

                    ASCIIEncoding ascii = new ASCIIEncoding();

                    s.Send(ascii.GetBytes("The string was received by the server."));
                    Console.WriteLine("\nSent Acknoledgement.");
                }
                while (l);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error message: " + e.Message);
                Console.WriteLine("Error stack tree : " + e.StackTrace);
            }
        }
    }
}
